﻿using Server.filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using DataHandler;
using Factory;


namespace Server.Controllers
{
    [CorsFilter]
    public class HomeController : Controller
    {
        //获取GetSql对象
        GetSql sqlcommond = AssemblyFactory.GetSqlAssmbly();


        //缓存所有站点的信息
        DataTable dt_allsites = new DataTable();
        // GET: /Home/
        public ActionResult Index()
        {
            var json = new
            {
                code = 1,
                result = "result"

            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        #region 地图下方轮播
        /// <summary>
        /// 全市平均降雨量
        /// </summary>
        /// <returns></returns>
        public ActionResult GetYiChangRain()
        {
            //查oracle中的所有站点的雨量值(早8点到当前数据，生序排列，最后一条最大)
            string datetime = DateTime.Now.ToString("yyyy-MM-dd")+" 08:00:00";
            DataTable dt_rain_ora = sqlcommond.GetYiChangRain(datetime);
            //雨量总和
            double rainsum = 0;
            //雨量平均值(保留两位小数)
            double rainval = 0.0;
            //遍历Oracle数据
            foreach (DataRow dr_ora in dt_rain_ora.Rows)
            {
                string rain = dr_ora["SUMVAL"].ToString();
                if (string.IsNullOrEmpty(rain))
                {
                    rain = "0";
                }
                rainsum += Convert.ToDouble(rain);

            }
            rainval = Math.Round(rainsum / dt_rain_ora.Rows.Count, 2);
            //最大雨量与最大雨量对应的站点
            double maxRain = 0.0;
            string maxSite = "";
            //ora的最大值
            if (dt_rain_ora.Rows.Count > 0) 
            {
                if (!string.IsNullOrEmpty(dt_rain_ora.Rows[dt_rain_ora.Rows.Count - 1]["SUMVAL"].ToString()))
                {
                    //if (maxRain < Convert.ToDouble(dt_rain_ora.Rows[dt_rain_ora.Rows.Count - 1]["SUMVAL"].ToString()))
                    //{
                    maxRain = Convert.ToDouble(dt_rain_ora.Rows[dt_rain_ora.Rows.Count - 1]["SUMVAL"].ToString());
                    maxSite = dt_rain_ora.Rows[dt_rain_ora.Rows.Count - 1]["stnm"].ToString();
                    //}
                }
            }
            var json = new
            {
                average = rainval,
                max = maxRain,
                maxsite = maxSite
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 重要江河水情查询
        /// </summary>
        /// <returns>json对象数组</returns>
        public ActionResult GetImportantJiangHeData()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetImportantJiangHeData();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 重要水库水情
        /// </summary>
        /// <returns></returns>
        public ActionResult GetImportantShuiKuData()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetImportantShuiKuData();
            DataTable dt_clon = dt.Clone();
            foreach (DataRow dr in dt.Rows) 
            {
                if (dr["STNM"].ToString().Contains("水库")) 
                {
                    int i = dr["STNM"].ToString().IndexOf('水');
                    dr["STNM"] = dr["STNM"].ToString().Substring(0,i);
                }
                if (dr["STNM"].ToString().Contains("(")) 
                {
                    int i = dr["STNM"].ToString().IndexOf('(');
                    dr["STNM"] = dr["STNM"].ToString().Substring(0, i);
                }
                dt_clon.Rows.Add(dr.ItemArray);
            }
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt_clon, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 全市城区积水预警信息（只给今天的数据，避免脏数据太多）
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCityWaterWarning()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetCityWaterWarning();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = "";
            json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 地图上方菜单
        /// <summary>
        /// 地下埋深
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDiXiaMaiShen()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetDiXiaMaiShen();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 土壤墒情
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTuRangShangQing()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetTuRangShangQing();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 水库水情
        /// </summary>
        /// <returns></returns>
        public ActionResult GetShuiKuShuiQing()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetShuiKuShuiQing();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 江河水情
        /// </summary>
        /// <returns></returns>
        public ActionResult GetJiangheShuiQing()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetJiangheShuiQing();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 城市积水
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCityJiShui()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetCityJiShui();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 城市河道
        /// </summary>
        /// <returns></returns>
        public ActionResult GetChengShihedao()
        {
            DataTable dt = new DataTable();
            dt = sqlcommond.GetChengShihedao();
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 县区平均雨量
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPingJunYuLiang()
        {
            //ZZ,ZQ的站点
            string datetime = DateTime.Now.ToString("yyyy-MM-dd") + " 08:00:00";
            DataTable dt = new DataTable();
            dt =sqlcommond.GetPingJunYuLiang(datetime);
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 县区平均雨量详情
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPingJunYuLiangdetail()
        {
            //ZZ,ZQ的站点
            string datetime = DateTime.Now.ToString("yyyy-MM-dd") + " 08:00:00";
            //DataTable dt = new DataTable();
            //dt = sqlcommond.GetPingJunYuLiangdetail(datetime);
            if (dt_allsites.Rows.Count <= 0)
            {
                dt_allsites = sqlcommond.GetAllSite1();
            }
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt_allsites, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 地图图标
        /// <summary>
        /// 所有站点信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllSite()
        {
            DataTable dt = new DataTable();
            if (dt_allsites.Rows.Count <= 0) 
            {
                dt_allsites = sqlcommond.GetAllSite1();
            }
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string json = JsonConvert.SerializeObject(dt_allsites, Newtonsoft.Json.Formatting.Indented, timeFormat);
            Response.ContentType = "application/json";
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 点击地图marker显示其信息
        /// </summary>
        /// <returns></returns>
        //public ActionResult ClickMapMarker(string stcd) 
        //{
        //    string datatime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        //    string datatime1 = DateTime.Now.ToString("yyyy-MM-dd");
        //    DataTable dt = new DataTable();
        //    dt = sqlcommond.ClickMapMarker(datatime,datatime1,stcd);
        //    IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
        //    timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
        //    string json = JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented, timeFormat);
        //    Response.ContentType = "application/json";
        //    return Json(json, JsonRequestBehavior.AllowGet);
        //}

        #endregion
    }
}
